Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports DotNetNuke.UI.Utilities

Namespace ModuleMasters.DNN.Modules.NCAAPoolBracket

    Public MustInherit Class NCAAResults

        Inherits Entities.Modules.PortalModuleBase
        Implements Entities.Modules.IActionable

#Region "Controls"
#End Region

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientReference(Me.Page, DotNetNuke.UI.Utilities.ClientAPI.ClientNamespaceReferences.dnn)

                'load the javascript that contains the client side scripts for picks
                Dim jsFilePath As String = ResolveUrl("ClientScript.js")
                Me.Page.RegisterClientScriptBlock("ClientScript.js", "<script src='" + jsFilePath + "'></script>")

                'declare some variables
                Dim ArrGameResult As ArrayList
                Dim GameController As New NCAAController

                If UserId > 0 Then
                    If Not Page.IsPostBack Then

                        lnkClearPicks.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to clear ALL Entered Results?');")

                        FormatBracket()
                        'load the results
                        ArrGameResult = GameController.GetResults(ModuleId)
                        LoadBracket(ArrGameResult)
                        AddEvents()
                        Me.lnkSubmitPicks.Visible = True
                        Me.lnkClearPicks.Visible = True
                    End If
                End If

            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Public Sub FormatBracket()
            'This proc is used to format the bracket
            Dim c As Control
            For Each c In Me.Controls
                If Not c.ID Is Nothing Then
                    If c.ID.Substring(0, 2) = "id" Then
                        Dim txt As System.Web.UI.WebControls.TextBox
                        txt = CType(c, TextBox)
                        txt.ReadOnly = True
                        txt.Width = Unit.Pixel(100)
                        txt.Font.Name = "Tahoma"
                        txt.Font.Size = FontUnit.Point(8)
                        txt.BorderStyle = BorderStyle.Groove
                    End If
                End If

            Next
        End Sub

        Public Sub AddEvents()
            'This proc is used to add our javascript events for clicking 
            Dim c As Control
            For Each c In Me.Controls
                If Not c.ID Is Nothing Then
                    If c.ID.Substring(0, 2) = "id" Then
                        Dim txt As System.Web.UI.WebControls.TextBox
                        txt = CType(c, TextBox)
                        If txt.ID <> "id127" Then 'add onclick except final game
                            txt.Attributes.Add("onclick", "ChangePick(this.name,this.value,'" & Me.ClientID & "_')")
                            txt.Attributes.Add("onmouseover", "this.style.cursor='hand'")
                        End If

                    End If
                End If
            Next
        End Sub

        Public Sub LoadBracket(ByVal arrGameResults As ArrayList)
            'load the team into the appropriate bracket slot
            Dim objGameResult As GameInfo
            Dim i As Integer
            For i = 0 To arrGameResults.Count - 1

                objGameResult = CType(arrGameResults(i), GameInfo)
                Dim c As Control
                For Each c In Me.Controls
                    If Not c.ID Is Nothing Then
                        If c.ID.Substring(0, 2) = "id" Then
                            Dim txt As System.Web.UI.WebControls.TextBox
                            txt = CType(c, TextBox)
                            If txt.ID = objGameResult.GameId Then
                                txt.Text = objGameResult.TeamName
                            End If

                            'Set the text of hidden controls
                            If CInt(c.ID.Substring(2, 3)) > 64 Then
                                Dim txthidden As System.Web.UI.HtmlControls.HtmlInputHidden
                                txthidden = Me.FindControl("ih" & c.ID.Substring(2, 3))
                                txthidden.Value = txt.Text
                            End If

                            If txt.ID = "id127" And objGameResult.GameId = "id127" Then
                                Me.txtActualScore.Text = objGameResult.GameScore
                            End If
                        End If
                    End If
                Next
            Next

        End Sub

        Private Sub lnkSubmitPicks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkSubmitPicks.Click
            'this will save a users pics to the DB, we will allow saving partial pics and will only score on what picks 
            'are there
            Dim GameController As New NCAAController

            'delete the previous picks
            'GameController.DeleteUserPrediction(ModuleId, UserId)

            'for each control that is a game id, call the insertprediction
            Dim c As Control
            For Each c In Me.Controls
                If Not c.ID Is Nothing Then
                    If c.ID.Substring(0, 2) = "ih" Then
                        'make sure we are not the first 64 gameids
                        If CInt(c.ID.Substring(2, 3)) > 64 Then
                            Dim objGameResult As New GameInfo
                            Dim txt As System.Web.UI.HtmlControls.HtmlInputHidden
                            txt = CType(c, HtmlInputHidden)
                            'set the values of the object
                            If txt.Value <> "" Then
                                objGameResult.GameId = "id" & txt.ID.Substring(2, 3)

                                'if we are game 127, then we set the final score
                                If txt.ID = "ih127" And Me.txtActualScore.Text.Length > 0 Then
                                    objGameResult.GameScore = Me.txtActualScore.Text
                                End If

                                objGameResult.TeamName = txt.Value

                                'call the insert now
                                GameController.InsertResults(PortalId, ModuleId, objGameResult.GameId, objGameResult.TeamName, objGameResult.GameScore, objGameResult.TeamAScore, objGameResult.TeambScore)

                            End If

                        End If
                    End If
                End If
            Next
            'redirect back to portal
            Response.Redirect(NavigateURL())
        End Sub

        Private Sub lnkClearPicks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkClearPicks.Click
            'ClearPicks()

            Dim GameController As New NCAAController
            GameController.ClearResults(PortalId, ModuleId)
            'redirect back to portal
            Response.Redirect(NavigateURL())
        End Sub

        ':TODO - Delete procedure. No longer needed.
        Private Sub ClearPicks()
            Dim c As Control
            For Each c In Me.Controls
                If Not c.ID Is Nothing Then
                    If c.ID.Substring(0, 2) = "id" Then
                        Dim txt As System.Web.UI.WebControls.TextBox
                        txt = CType(c, TextBox)
                        'see if we are > gameid 64
                        If CInt(c.ID.Substring(2, 3)) > 64 Then
                            txt.Text = ""
                            'Clear hidden text
                            Dim txthidden As System.Web.UI.HtmlControls.HtmlInputHidden
                            txthidden = Me.FindControl("ih" & c.ID.Substring(2, 3))
                            txthidden.Value = ""
                        End If
                    End If
                End If
            Next
            'now clear the final score
            Me.txtActualScore.Text = ""
        End Sub

#End Region

#Region "Optional Interfaces"
        Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
                'Actions.Add(GetNextActionID, Localization.GetString(Entities.Modules.Actions.ModuleActionType.AddContent, LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                'Actions.Add(Getnextactionid, "Game Results", "GameResults", "", "", "NCAAResults.ascx", False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property

#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents id003 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id004 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id006 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id007 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id001 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id002 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id005 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id065 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id097 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id113 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id121 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id125 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id127 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id066 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id067 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id098 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id068 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id008 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id009 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id069 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id099 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id114 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id010 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id011 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id070 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id012 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id013 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id071 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id100 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id014 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id015 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id072 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id016 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id017 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id073 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id101 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id115 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id122 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id018 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id019 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id074 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id020 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id021 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id075 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id102 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id022 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id023 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id076 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id024 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id025 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id077 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id103 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id116 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id026 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id027 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id078 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id028 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id029 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id079 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id104 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id030 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id031 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id080 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id032 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id033 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id081 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id105 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id117 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id123 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id126 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id034 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id035 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id082 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id036 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id037 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id083 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id106 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id038 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id039 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id084 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id040 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id041 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id085 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id107 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id118 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id042 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id043 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id086 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id044 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id045 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id087 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id108 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id046 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id047 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id088 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id048 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id049 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id089 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id109 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id119 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id124 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id050 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id051 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id090 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id052 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id053 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id091 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id110 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id054 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id055 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id092 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id056 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id057 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id093 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id111 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id120 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id058 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id059 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id094 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id060 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id061 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id095 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id112 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id062 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id063 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id096 As System.Web.UI.WebControls.TextBox
        Protected WithEvents id064 As System.Web.UI.WebControls.TextBox
        Protected WithEvents lblFinalScore As System.Web.UI.WebControls.Label
        Protected WithEvents lnkSubmitPicks As System.Web.UI.WebControls.LinkButton
        Protected WithEvents lnkClearPicks As System.Web.UI.WebControls.LinkButton
        Protected WithEvents lblCombinedScore As System.Web.UI.WebControls.Label
        Protected WithEvents lblActualScore As System.Web.UI.WebControls.Label
        Protected WithEvents txtActualScore As System.Web.UI.WebControls.TextBox
        Protected WithEvents ih065 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih066 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih067 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih068 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih069 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih070 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih071 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih072 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih073 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih074 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih075 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih076 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih077 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih078 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih079 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih080 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih081 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih082 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih083 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih084 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih085 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih086 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih087 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih088 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih089 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih090 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih091 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih092 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih093 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih094 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih095 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih096 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih097 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih098 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih099 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih100 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih101 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih102 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih103 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih104 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih105 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih106 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih107 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih108 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih109 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih110 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih111 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih112 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih113 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih114 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih115 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih116 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih117 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih118 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih119 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih120 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih121 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih122 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih123 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih124 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih125 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih126 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents ih127 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents valTxtActualScore As System.Web.UI.WebControls.RegularExpressionValidator

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region




    End Class

End Namespace
